/**
 * 
 */
package web;

import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.application.FacesMessage.Severity;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;

import org.springframework.http.HttpMethod;

/**
 * @author Administrador
 * 
 */
public abstract class BaseManagedBean {
    
    	public static int QTDA_CLASSE = 0;
    

	@PostConstruct
	public void postConstruct() {
	    QTDA_CLASSE++;
	    System.out.println("+++++++++ Inicializou a Classe " + this.getClass().getSimpleName() + " ==== " + QTDA_CLASSE);
	    inicializar();
	}
	
	    public String acaoStr() {
		System.out.println("###### acaoStr classe: " + getClass().getName() + " ==== " + QTDA_CLASSE);
		return null;
	    }

	    public void acao() {
		System.out.println("###### acao classe: " + getClass().getName() + " ==== " + QTDA_CLASSE);
	    }
	
	protected void inicializar() {};

	protected HttpServletRequest getRequest() {
		HttpServletRequest request = (HttpServletRequest) getFacesContext()
				.getExternalContext().getRequest();
		return request;
	}

	protected boolean isHTTPGet() {
		return getRequest().getMethod()
				.equals(HttpMethod.GET.name().toString());
	}

	protected String getParametroRequest(String nome) {
		return (String) getRequestParam().get(nome);
	}

	protected void addErrorMessage(String componentId, String errorMessage) {
		addMessage(componentId, errorMessage, FacesMessage.SEVERITY_ERROR);
	}

	protected void addErrorMessage(String errorMessage) {
		addErrorMessage(null, errorMessage);
	}

	protected void addInfoMessage(String componentId, String infoMessage) {
		addMessage(componentId, infoMessage, FacesMessage.SEVERITY_INFO);
	}

	protected void addAlertMessage(String infoMessage) {
		addMessage(null, infoMessage, FacesMessage.SEVERITY_WARN);
	}

	protected void addInfoMessage(String infoMessage) {
		addInfoMessage(null, infoMessage);
	}

	private void addMessage(String componentId, String errorMessage,
			Severity severity) {
		FacesMessage message = new FacesMessage(errorMessage);
		message.setSeverity(severity);
		FacesContext.getCurrentInstance().addMessage(componentId, message);
	}

	public void addMessageDetail(String componentId, String errorMessage,
			Severity severity, String detail) {
		FacesMessage message = new FacesMessage(errorMessage, detail);
		message.setSeverity(severity);
		FacesContext.getCurrentInstance().addMessage(componentId, message);
	}

	public Map getApplicationScope() {
		return getFacesContext().getExternalContext().getApplicationMap();
	}

	public FacesContext getFacesContext() {
		return FacesContext.getCurrentInstance();
	}

	public Map getRequestParam() {
		return getFacesContext().getExternalContext().getRequestParameterMap();
	}

	public Map getRequestScope() {
		return getFacesContext().getExternalContext().getRequestMap();
	}

	public Map getSessionScope() {
		return getFacesContext().getExternalContext().getSessionMap();
	}

	@SuppressWarnings("unchecked")
	public void putSession(String param, Object value) {
		getSessionScope().put(param, value);
	}

	@SuppressWarnings("unchecked")
	public void putRequest(String param, Object value) {
		getRequestScope().put(param, value);
	}

	public Map getSessionMap() {
		return FacesContext.getCurrentInstance().getExternalContext()
				.getSessionMap();
	}
}
